/**
 * 作者: wgx
 * 日期: 2024-12-04 17:23:45
 * 备注: 物资争夺名人广播
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameFight.PBServerSnatchFame;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerSnatchFameModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // ID
    protected int serverId; // 服务器id
    protected long dateTime; // 这天的0点0分,清除历史数据用
    protected long attactId; // 进攻方的userid
    protected int attactLevel; // 进攻方的level
    protected String attactName = ""; // 进攻方的name
    protected long defendId; // 防守方的userid
    protected String defendName = ""; // 防守方的name
    protected int koNum; // 击败人数
    protected long fightTime; // 战斗时间

    public void reset() {
    }

    protected String getKey() {
        return "ServerSnatchFame." + id;
    }

    protected String getListKey() {
        return "ServerSnatchFame." + serverId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_snatch_fame"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("serverId", serverId); // 服务器id
        obj.put("dateTime", dateTime); // 这天的0点0分,清除历史数据用
        obj.put("attactId", attactId); // 进攻方的userid
        obj.put("attactLevel", attactLevel); // 进攻方的level
        obj.put("attactName", attactName); // 进攻方的name
        obj.put("defendId", defendId); // 防守方的userid
        obj.put("defendName", defendName); // 防守方的name
        obj.put("koNum", koNum); // 击败人数
        obj.put("fightTime", fightTime); // 战斗时间
        return obj;
    }

    public PBServerSnatchFame.Builder takeInitBuilder() {
        PBServerSnatchFame.Builder sb = PBServerSnatchFame.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (attactId != 0) {
            sb.setAttactId(attactId);
        }
        if (attactLevel != 0) {
            sb.setAttactLevel(attactLevel);
        }
        if (!attactName.isEmpty()) {
            sb.setAttactName(attactName);
        }
        if (defendId != 0) {
            sb.setDefendId(defendId);
        }
        if (!defendName.isEmpty()) {
            sb.setDefendName(defendName);
        }
        if (koNum != 0) {
            sb.setKoNum(koNum);
        }
        return sb;
    }

    public PBServerSnatchFame.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerSnatchFame.Builder sb = PBServerSnatchFame.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("attactId")) {
            sb.setAttactId(attactId);
        }
        if (isFieldUpdate("attactLevel")) {
            sb.setAttactLevel(attactLevel);
        }
        if (isFieldUpdate("attactName")) {
            sb.setAttactName(attactName);
        }
        if (isFieldUpdate("defendId")) {
            sb.setDefendId(defendId);
        }
        if (isFieldUpdate("defendName")) {
            sb.setDefendName(defendName);
        }
        if (isFieldUpdate("koNum")) {
            sb.setKoNum(koNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public long getDateTime() {
        return dateTime;
    }

    public void putDateTime(long dateTime) {
        if (this.dateTime != dateTime) {
            this.dateTime = dateTime;
            updateRedisHashField(getKey(), "dateTime", this.dateTime);
        }
    }

    public void addDateTime(long value) {
        this.dateTime += value;
        updateRedisHashField(getKey(), "dateTime", this.dateTime);
    }

    public long getAttactId() {
        return attactId;
    }

    public void putAttactId(long attactId) {
        if (this.attactId != attactId) {
            this.attactId = attactId;
            updateRedisHashField(getKey(), "attactId", this.attactId);
        }
    }

    public void addAttactId(long value) {
        this.attactId += value;
        updateRedisHashField(getKey(), "attactId", this.attactId);
    }

    public int getAttactLevel() {
        return attactLevel;
    }

    public void putAttactLevel(int attactLevel) {
        if (this.attactLevel != attactLevel) {
            this.attactLevel = attactLevel;
            updateRedisHashField(getKey(), "attactLevel", this.attactLevel);
        }
    }

    public void addAttactLevel(int value) {
        this.attactLevel += value;
        updateRedisHashField(getKey(), "attactLevel", this.attactLevel);
    }

    public String getAttactName() {
        return attactName;
    }

    public void putAttactName(String attactName) {
        if (!this.attactName.equals(attactName)) {
            this.attactName = attactName;
            updateRedisHashField(getKey(), "attactName", this.attactName);
        }
    }

    public long getDefendId() {
        return defendId;
    }

    public void putDefendId(long defendId) {
        if (this.defendId != defendId) {
            this.defendId = defendId;
            updateRedisHashField(getKey(), "defendId", this.defendId);
        }
    }

    public void addDefendId(long value) {
        this.defendId += value;
        updateRedisHashField(getKey(), "defendId", this.defendId);
    }

    public String getDefendName() {
        return defendName;
    }

    public void putDefendName(String defendName) {
        if (!this.defendName.equals(defendName)) {
            this.defendName = defendName;
            updateRedisHashField(getKey(), "defendName", this.defendName);
        }
    }

    public int getKoNum() {
        return koNum;
    }

    public void putKoNum(int koNum) {
        if (this.koNum != koNum) {
            this.koNum = koNum;
            updateRedisHashField(getKey(), "koNum", this.koNum);
        }
    }

    public void addKoNum(int value) {
        this.koNum += value;
        updateRedisHashField(getKey(), "koNum", this.koNum);
    }

    public long getFightTime() {
        return fightTime;
    }

    public void putFightTime(long fightTime) {
        if (this.fightTime != fightTime) {
            this.fightTime = fightTime;
            updateRedisHashField(getKey(), "fightTime", this.fightTime);
        }
    }

    public void addFightTime(long value) {
        this.fightTime += value;
        updateRedisHashField(getKey(), "fightTime", this.fightTime);
    }

}